<?php
    //require section
    require_once dirname(dirname(__FILE__)) . '/shared/config/config.php';
    require_once DOCUMENT_ROOT . DIR_INCLUDE . '/check_user_login.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_API . '/api_document.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_API . '/api_invoice.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . '/lib_pager.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . '/lib_upload.php';
    $title = 'Admin Panel';

    $id = 0;
    $email = "";
    $first_name = "";
    $last_name = "";
    $address = "";
    $phone = "";
    $file = "";
    $action = "Add";
    $action_show = "Thêm";

    if (!empty($_POST)) {

        if (!empty($_POST['Action']) && $_POST['Action'] === "Process") {
            echo "Dsgsgsd";
            $id_process = 0;
            if (!empty($_POST['process_id'])) {
                $id_process = $_POST['process_id'];
            }
            api_invoice::process_invoice($id_process);
            $success_info = "Đã xử lý đơn hàng " . $id_process . " thành công";
        } else if (!empty($_POST['Action']) && $_POST['Action'] === "Delete") {
            $id_delete = 0;
            if (!empty($_POST['delete_id'])) {
                $id_delete = $_POST['delete_id'];
            }
            api_invoice::delete_invoice($id_delete);
            $success_info = "Xóa đơn hàng thành công";
        }
    }



?>

<!DOCTYPE html>
<html>
<head>
    <?php
        require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'head.php';
    ?>
</head>
<body>
<div class="uk-container uk-container-center uk-margin-top">
    <?php
        require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'menu.php';
    ?>

    <section class="ts-body-container uk-panel uk-panel-box ts-padding-large">
        <?php
            if (!empty($success_info)) {
                ?>
                <div class="success"><?php echo $success_info; ?></div>
            <?php
            }?>

        <hr/>
        <h2><strong>Chi tiết đơn hàng</strong></h2>

        <form action="?" method="POST" id="form_delete">
            <input type="hidden" name="delete_id" value="0"/>
            <input type="hidden" name="Action" value="Delete"/>
        </form>
        <form action="?" method="POST" id="form_process">
            <input type="hidden" name="process_id" value="0"/>
            <input type="hidden" name="Action" value="Process"/>
        </form>

        <table class="uk-table uk-table-hover uk-table-striped uk-table-condensed">
            <thead>
            </thead>
            <tbody>
            <?php
                $invoice_id = 0;
                if (!empty($_GET['invoice_id'])) {
                    $invoice_id = $_GET['invoice_id'];
                }
                $invoice_info = api_invoice::get_invoice_by_id($invoice_id);
            ?>
            <tr>
                <td><strong>Mã đơn </strong></td>
                <td><?php echo $invoice_info['invoice_id']; ?></td>
            </tr>
            <tr>
                <td><strong>Ngày tạo</strong></td>
                <?php
                $date = date_create($invoice_info["created_date"]);
                $date_str = date_format($date, "d/m/Y");
                ?>
                <td><?php echo $date_str; ?></td>
            </tr>
            <tr>
                <td><strong>Trạng thái</strong></td>
                <td><?php echo $invoice_info['status_name']; ?></td>
            </tr>
            <tr>
                <td><strong>Tổng số lượng</strong></td>
                <td><?php echo $invoice_info['total_quantity']; ?></td>
            </tr>
            <tr>
                <td><strong>Tổng tiền</strong></td>
                <td><?php echo number_format($invoice_info['total_money'], 0, '.', ',') . " vnđ"; ?></td>
            </tr>
            <tr>
                <td><strong>Tên khách hàng</strong></td>
                <td><?php echo $invoice_info['customer_name']; ?></td>
            </tr>
            <tr>
                <td><strong>Email</strong></td>
                <td><?php echo $invoice_info['customer_email']; ?></td>
            </tr>
            <tr>
                <td><strong>Số điện thoại</strong></td>
                <td><?php echo $invoice_info['customer_phone']; ?></td>
            </tr>
            </tbody>
        </table>

        <h3><strong>Các món hàng</strong></h3>
        <table class="uk-table uk-table-hover uk-table-striped uk-table-condensed">
            <thead>
            <tr>
                <th>STT</th>
                <th>Mã bài tập</th>
                <th>Tên bài tập</th>
                <th>Giá</th>
                <th>Số Lượng</th>
            </tr>
            </thead>
            <tbody>
            <?php
                $current_page = 1;
                $page_size = 10;
                if (!empty($_GET['p'])) {
                    $current_page = $_GET['p'];
                }


                $list_total = api_invoice::get_all_lines_by_invoice_id($invoice_id);
                $lib_pager = new lib_pager();
                $total_page = $lib_pager->get_total_page($list_total, $page_size);
                $current_page_list = $lib_pager->get_current_page_list($page_size, $current_page, $list_total);

                $count = 0;
                if (empty($current_page_list) || count($current_page_list) == 0) {
                    echo "<tr><td>chưa có món hàng nào</td></td>";
                } else {
                    foreach ($current_page_list as $i => $item) {
                        ?>
                        <tr>
                            <td><?php echo $i + 1; ?></td>
                            <td><?php echo $item['doc_id'] ?></td>
                            <td><?php echo $item['doc_name'] ?></td>
                            <td><?php echo number_format($item['money'], 0, '.', ',') ?></td>
                            <td><?php echo $item['quantity'] ?></td>
                        </tr>
                    <?php
                    }
                } ?>
            <tr align="right" style="color:Black;background-color:#F7F7DE;">
                <td colspan="8">
                    <table>
                        <tbody>
                        <tr align="right" style="color:Black;background-color:#F7F7DE;">
                            <td colspan="7">
                                <table>
                                    <tbody>
                                    <tr>
                                        <?php for ($a = 1; $a <= $total_page; $a++) { ?>
                                            <td>
                                                                                <span>
                                                                                    <a href="?p=<?php echo $a; ?>" <?php if ($current_page != $a) echo 'style="color:Black;"'; ?>><?php echo $a; ?></a>
                                                                                </span>
                                            </td>
                                        <?php } ?>
                                    </tr>
                                    </tbody>
                                </table>
                            </td>
                        </tr>
                        </tbody>
                    </table>
                </td>
            </tr>
            </tbody>
        </table>
    </section>

    <?php
        require_once DOCUMENT_ROOT . DIR_INCLUDE . 'footer.php';
    ?>
</div>
</body>
</html>
